﻿Public Class FormType

    Private Sub FormType_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(dgvType)
        LoadData()
    End Sub

    Public Sub LoadData()
        Me.dgvType.DataSource = Cmd.ExecuteDataTable("SELECT * FROM Type WHERE UPPER(TypeName) LIKE UPPER(N'%" & txtSearch.Text & "%') AND Active=1")
    End Sub

    Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        LoadData()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim frm As New FormTypeEditor()
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            LoadData()
        End If
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If Me.dgvType.SelectedRows.Count = 0 Then
            Exit Sub
        End If


        Dim row As DataGridViewRow = Me.dgvType.SelectedRows(0)
        Dim id = row.Cells("TypeId").Value

        Cmd.Parameters("@Id") = id
        Dim count = Cmd.ExecuteScalar("SELECT COUNT(*) FROM Category WHERE TypeId=@Id AND Active=1")

        If count > 0 Then
            MsgBox("Type is already in used")
            Exit Sub
        End If

        If MessageBox.Show("តើលោកអ្នកចង់លុបប្រភេទនៃក្រុមមួយនេះមែនទេ?", "លុបប្រភេទនៃក្រុម", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
            Exit Sub
        End If

        Cmd.ExecuteNonQuery("UPDATE Type SET Active=0 WHERE TypeId=" & id)

        LoadData()
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        If Me.dgvType.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        Dim row As DataGridViewRow = dgvType.SelectedRows(0)
        Dim TypeId = row.Cells("TypeId").Value
        Dim TypeName = row.Cells("TypeName").Value

        Dim frm As New FormTypeEditor()
        frm.txtTypeId.Enabled = False
        frm.txtTypeId.Text = TypeId
        frm.txtTypeName.Text = TypeName

        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            LoadData()
        End If
    End Sub

    Private Sub txt1_TextChanged(sender As Object, e As EventArgs) Handles txt1.TextChanged
        Dim test As Date
        If Date.TryParseExact(txt1.Text.ToString(), "yyyy/mm/dd", _
                              System.Globalization.CultureInfo.CurrentCulture, _
                              Globalization.DateTimeStyles.None, test) Then
            MessageBox.Show("Ok")

        Else

        End If
    End Sub

    Private Sub dgvType_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvType.CellContentClick

    End Sub
End Class